// pages/mall/index.js
import api from '../../api/index'
import { AppId } from '../../api/config'
import { userInfoStore } from '../../store/userInfo'
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLevel: 0,
    banners: [],
    select:0,
    tab: [
      { title: '家用中央空调', image: '../../images/mall/icon_mall_tab1.png' },
      { title: '家用分体空调', image: '../../images/mall/icon_mall_tab2.png' },
      { title: '空气净化', image: '../../images/mall/icon_mall_tab3.png' },
      { title: '维护保养', image: '../../images/mall/icon_mall_tab4.png' },
      { title: '积分兑换', image: '../../images/mall/icon_mall_tab5.png' }
      ],
    product:[],
    uploading: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    api.getbanner({ pageNo: 3 }).then(res => {
      self.setData({
        banners: res.result
      })
    })

    api.getDisplayProductType().then(res => {
      self.setData({select: res.result},()=>{
        self.getProduct(1+res.result);
      })
      
    })
    // self.getProduct(1);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ userLevel: userInfoStore.userInfo.level })
    let giftBag = this.selectComponent("#giftbag");
    getApp().displayGiftBag(giftBag);
  },

  getProduct:function(t){
    let self=this
    api.getproductlist({ type:t}).then(res=>{
      self.setData({ product:res.result});
    })
  },

  switchtab:function(e){
    let idx = e.currentTarget.dataset.index;
    if (idx == 4) {
      wx.showToast({
        icon: "none",
        title: "敬请期待!"
      })
      return
    }

    let self = this
    this.setData({ select: idx, product:[]},()=>{
      self.getProduct(idx + 1);
    });
  },

  exchange: function (e) {
    let pid=e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '../product/detail?id='+pid,
    })
  },

  showTips:function(e){
    wx.showToast({
      icon: "none",
      title: "敬请期待!"
    })
  },

  getPhoneNumber: function (e) {
    // console.log(e);
    if (e.detail.iv) {
      let { session_key } = userInfoStore.userInfo;
      let pc = new WXBizDataCrypt(AppId, session_key);
      var ret = pc.decryptData(e.detail.encryptedData, e.detail.iv)
      console.log('解密后 data: ', ret)

      var param = {
        openId: userInfoStore.userInfo.openId,
        phone: ret.phoneNumber,
        level: 1
      }
      var self = this
      api.saveUser(param).then(res => {
        let { userInfo } = userInfoStore;
        let { result } = res;
        result.session_key = userInfo.session_key;
        userInfoStore.updateUserInfo(result);
        wx.showModal({
          title: "提交成功",
          showCancel: false,
          confirmText: "我知道了",
          success: function (res) {
            if (res.confirm) {
              self.setData({
                userLevel: userInfoStore.userInfo.level
              });
            }
          }
        })
      }, err => {
        wx.showModal({
          title: "提交失败",
          content: err,
          showCancel: false,
          confirmText: "我知道了"
        })
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().share();
  }
})